package com.lynkin.weather.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lynkin.weather.constant.ResultConstant;
import com.lynkin.weather.util.HttpClientUtil;
import com.lynkin.weather.util.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Iterator;

/**
 * service
 */
@Service
public class WeatherService {

    @Autowired
    private RedisUtils redisUtils;

    private static Logger log = LoggerFactory.getLogger(WeatherService.class);

    /**
     * 获得区县气温
     * @param province 省
     * @param city 市
     * @param county 区县
     * @return
     */
    public String getTemperature(String province, String city, String county) {

        //返回省份错误
        if (StringUtils.isBlank(province)) {
            return ResultConstant.ERROR_PROVINCE;
        }
        //返回市错误
        if (StringUtils.isBlank(city)) {
            return ResultConstant.ERROR_CITY;
        }
        //返回区县错误
        if (StringUtils.isBlank(county)) {
            return ResultConstant.ERROR_COUNTY;
        }

        //如果未缓存省份数据
        if (!redisUtils.exists("provinceCode")) {
            //省份api
            String provinceUrl = "http://www.weather.com.cn/data/city3jdata/china.html";
            //省份信息
            String provinceInfos = null;
            try {
                provinceInfos = HttpClientUtil.get(provinceUrl);
            } catch (IOException e) {
                e.printStackTrace();
                log.error("请求异常", e.getMessage());
            }

            redisUtils.set("provinceCode", provinceInfos);
        }

        //获得省份code
        String provinceCode = getProvinceCode(province);
        //返回省份错误
        if (StringUtils.isBlank(provinceCode)) {
            return ResultConstant.ERROR_PROVINCE;
        }


        //如果未缓存该省份下地级市数据
        if (!redisUtils.exists(provinceCode)) {
            //地级市api
            String cityUrl = "http://www.weather.com.cn/data/city3jdata/provshi/" + provinceCode + ".html";
            //地级市信息
            String cityInfos = null;
            try {
                cityInfos = HttpClientUtil.get(cityUrl);
            } catch (IOException e) {
                e.printStackTrace();
                log.error("请求异常", e.getMessage());
            }
            redisUtils.set(provinceCode, cityInfos);
        }
        //获得地级市code
        String cityCode = getCityCode(city, provinceCode);
        //返回市错误
        if (StringUtils.isBlank(cityCode)) {
            return ResultConstant.ERROR_CITY;
        }


        //省code + 市code
        String provinceCityCode = provinceCode + cityCode;

        //如果未缓存该地级市下区县数据
        if (!redisUtils.exists(provinceCityCode)) {
            //区县api
            String countyUrl = "http://www.weather.com.cn/data/city3jdata/station/" + provinceCityCode + ".html";
            //区县信息
            String countyInfos = null;
            try {
                countyInfos = HttpClientUtil.get(countyUrl);
            } catch (IOException e) {
                e.printStackTrace();
                log.error("请求异常", e.getMessage());
            }
            redisUtils.set(provinceCityCode, countyInfos);
        }
        //获得区县code
        String countyCode = getCountyCode(county, provinceCityCode);
        //返回区县错误
        if (StringUtils.isBlank(countyCode)) {
            return ResultConstant.ERROR_COUNTY;
        }

        String totalCode = provinceCityCode + countyCode;

        //天气api
        String weatherUrl = "http://www.weather.com.cn/data/sk/" + totalCode + ".html";
        //天气信息
        String weatherInfos = null;
        try {
            weatherInfos = HttpClientUtil.get(weatherUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONUtil.parseObj(weatherInfos).getJSONObject("weatherinfo");
        if (jsonObject == null) return null;
        return jsonObject.get("temp") == null ? null : (String) jsonObject.get("temp");
    }


    /**
     * 获得省份code
     * @param province
     * @return
     */
    public String getProvinceCode(String province) {
        //获取redis的省份数据
        JSONObject jsonObject = JSONUtil.parseObj(redisUtils.get("provinceCode"));
        Iterator<String> provinceIterator = jsonObject.keySet().iterator();
        while (provinceIterator.hasNext()) {
            String code = provinceIterator.next();
            if (province.equals(jsonObject.get(code))) {
                return code;
            }
        }
        return null;
    }


    /**
     * 获得地级市code
     * @param city
     * @return
     */
    public String getCityCode(String city, String provinceCode) {
        //获取该省份下的地级市数据
        JSONObject jsonObject = JSONUtil.parseObj(redisUtils.get(provinceCode));
        Iterator<String> cityIterator = jsonObject.keySet().iterator();
        while (cityIterator.hasNext()) {
            String code = cityIterator.next();
            if (city.equals(jsonObject.get(code))) {
                return code;
            }
        }
        return null;
    }

    /**
     * 获得区县code
     * @param provinceCityCode
     * @return
     */
    public String getCountyCode(String county, String provinceCityCode) {
        //获取该地级市下的区县数据
        JSONObject jsonObject = JSONUtil.parseObj(redisUtils.get(provinceCityCode));
        Iterator<String> countyIterator = jsonObject.keySet().iterator();
        while (countyIterator.hasNext()) {
            String code = countyIterator.next();
            if (county.equals(jsonObject.get(code))) {
                return code;
            }
        }
        return null;
    }



}
